package com.ddm.controller.wxadmin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ddm.constant.MessageConstant;
import com.ddm.dto.vxDTO.ScoreDTO;
import com.ddm.entity.vxEntity.Score;
import com.ddm.exception.DataErrorException;
import com.ddm.mapper.ScoreMapper;
import com.ddm.result.Result;
import com.ddm.service.vxService.ScoreService;
import com.ddm.vo.vxVo.ContestVo;
import com.ddm.vo.vxVo.ScoreVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author horizon
 * @since 2025-01-17
 */
@RestController
@RequestMapping("/admin/score")
@Slf4j
@Api("成绩管理接口")
public class ScoreController {
    @Autowired
    ScoreService scoreService;

    @GetMapping("/exam/{id}")
    @ApiOperation("根据考试id查询成绩")
    public Result<ContestVo> getScore(@PathVariable Long id){
        log.info("根据考试id查询成绩");
        if (id==null){
            throw new DataErrorException(MessageConstant.NULL_DATA);
        }
        return Result.success(scoreService.getScore(id));
    }

    @GetMapping("/{id}")
    @ApiOperation("根据用户id查询所有考试成绩")
    public Result<List<ScoreVO>> getUserScore(@PathVariable Long id){
        log.info("根据用户id查询所有考试成绩");
        if (id ==null){
            throw new DataErrorException(MessageConstant.NULL_DATA);
        }
        return scoreService.getUserScore(id);
    }

    @DeleteMapping
    @ApiOperation("根据考试id与用户id删除考试成绩")
    public Result<String> deleteScore(@RequestBody ScoreDTO scoreDTO){
        log.info("根据用户id与考试id删除考试成绩");
        if (scoreDTO.getExamId()==null ||scoreDTO.getUserId() ==null){
            throw new DataErrorException(MessageConstant.NULL_DATA);
        }
        LambdaQueryWrapper<Score> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Score::getUserId,scoreDTO.getUserId()).eq(Score::getExamId,scoreDTO.getExamId());
        Score score = scoreService.getOne(wrapper);
        if (score == null){
            throw new DataErrorException(MessageConstant.ERROR_DATA);
        }
        scoreService.removeById(score.getId());
        return Result.success("删除成功");
    }

}

